<template>
    <el-form :model="shop" label-position="right" label-width="auto">
        <el-row :gutter="10">
            <el-col :span="12">
                <el-form-item label="店名" prop="shopname">
                    <el-input v-model="shop.shopname" placeholder="请输入店名" clearable/>
                </el-form-item>
            </el-col>
            <el-col :span="12">
                <el-form-item label="密码" prop="pwd">
                    <el-input type="password" v-model="shop.pwd" placeholder="请输入密码" clearable/>
                </el-form-item>
            </el-col>
        </el-row>

        <el-row :gutter="10">
            <el-col :span="12">
                <el-form-item label="地址" prop="address">
                    <el-input v-model="shop.address" placeholder="请输入地址" clearable/>
                </el-form-item>
            </el-col>
        </el-row>

        <el-row :gutter="10">
            <el-col :span="12">
                <el-form-item label="电话" prop="phone">
                    <el-input v-model="shop.phone" placeholder="请输入电话" clearable/>
                </el-form-item>
            </el-col>
        </el-row>
        <el-row :gutter="10">
            <el-col :span="12">
                <el-form-item label="配送费" prop="gratuity">
                    <el-input v-model="shop.gratuity" placeholder="请输入配送费" clearable/>
                </el-form-item>
            </el-col>
        </el-row>

        <el-form-item label="头像" prop="picture">
            <el-upload drag style="width:100%"
                       action="http://localhost:8081/show"
                       :on-success="handleFileUploadSuccess"
            >
                <el-icon class="el-icon--upload"><upload-filled/></el-icon>
                <div class="el-upload__text">将文档拖拽到此处，或 <em>点击上传</em></div>
                <template #tip>
                    <div class="el-upload__tip">支持 jpg/png 格式， 大小不超过 5M</div>
                </template>
            </el-upload>
        </el-form-item>
    </el-form>

    <el-row>
        <el-col>
            <div style="text-align: center;">
                <el-button type="primary" @click="doSave">保存</el-button>
                <el-button @click="doCancel">取消</el-button>
            </div>
        </el-col>
    </el-row>
</template>

<script setup lang="ts">
import { inject, ref, onMounted } from 'vue'
import { ElMessage } from "element-plus";
import axios from 'axios';
import {useRoute} from "vue-router";
import storage from "../../../storage.ts";

//---------------------------- 自定义组件事件 -----------------------------
const emits = defineEmits([
    "flushPage"		// 刷新页面
]);

// 获取当前店铺ID
const shopId = localStorage.getItem('shopInfo') ? JSON.parse(localStorage.getItem('shopInfo')).id : null;
const shopPwd=ref("");
if (!shopId) {
    ElMessage.error('未找到店铺信息，请先登录');
}

const shop = ref({
    id: shopId,
    shopname: "",
    pwd: "" ,
    phone: "",
    address: "",
    picture: "",
    checked: "",
    state: "",
    income: "",
    score: "",
    gratuity: ""
})



// 获取当前登录用户信息
const route = useRoute();

const user = ref();
// 初始化时获取店铺信息
onMounted(async () => {

    user.value=storage.getUser();
    storage.removeUser();

    try {
        // 优先从路由参数中获取店铺ID
        const shopId = route.query.id;
        if (shopId) {
            // 调用后端接口获取店铺信息
            const { data } = await axios.get(`/api/shop/${shopId}`);
            shop.value = data;
            console.log(shop.value);

        } else {
            // 如果没有路由参数，则尝试从localStorage中获取
            const storedShopInfo = localStorage.getItem('shopInfo');
            if (storedShopInfo) {
                shop.value = JSON.parse(storedShopInfo);

            }
        }
    } catch (error) {
        console.error('获取店铺信息失败:', error);
    }
});

const handleFileUploadSuccess = (fileName) => {
    if (fileName != null || fileName != '') {
        shop.value.picture = fileName;
        ElMessage({ type: 'success', message: '文件上传成功' });
    } else {
        ElMessage({ type: 'error', message: '文件上传失败' });
    }
}

// 取消
const doCancel = () => {
    emits("closeDialog");
}

// 更新
const doSave = async () => {
    try {
        const { data } = await axios.put('/api/shop', shop.value); // 修改为更新当前店铺信息的接口
        location.href = '/multilogin';
    } catch (error) {
        ElMessage.error("保存失败，请稍后重试！");
    }
}
</script>

<style scoped>
</style>